import JpushModule from "jpush-react-native";
import storage from "../util/storage";
import { USER_ID } from "../util/Const";
import { Platform } from "react-native";

function initJPush(alias = storage.get(USER_ID)) {
  JpushModule.clearAllNotifications();
  if (Platform.OS == "ios") JpushModule.setBadge(0, success => {
    console.log("setBadge success =>" + success);
  });
  JpushModule.setAlias(alias, success => {
    console.log("setAlias success => " + storage.get(USER_ID) + success);
  });

  JpushModule.addReceiveNotificationListener(event => {
    // console.log(JSON.stringify(event));
    if (Platform.OS == "ios")
      JpushModule.getBadge(badge => {
        JpushModule.setBadge(badge + 1);
      });
  });

  JpushModule.resumePush();
}
export default initJPush;
